<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "@/views/dashboard/mixins/resize.js";

const animationDuration = 6000;

export default {
  mixins: [resize],
  props: {
    title: {
      type: Object,
      default: {},
    },
    xAxis: {
      type: Array,
      default: [],
    },
    yAxis: {
      type: Array,
      default: [],
    },
    toolbox: {
      type: Object,
      default: {},
    },
    quotaIds: {
      type: Array,
      default: [],
    },
    legend: {
      type: Array,
      default: [],
    },
    data: {
      type: Array,
      default: [],
    },
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
  },
  data() {
    return {
      chart: null,
      series: [],
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
    
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.quotaIds.forEach((item, index) => {
      this.series.push({
        name: this.legend[index],
        type: "bar",
        stack: "vistors",
        barWidth: "60%",
        data: this.data[this.quotaIds[index]],
        animationDuration,
      });
    });
      this.chart.setOption({
        title: this.title,
        grid: {
          top: 60,
          width: 850,
          height: 250,
          left: "2%",
          right: "2%",
          bottom: "3%",
          containLabel: true,
        },
        legend: {
          data: this.legend,
        },
        toolbox: this.toolbox,
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
          },
        },

        xAxis: this.xAxis,
        yAxis: this.yAxis,
        series: this.series,
      });
    },
  },
};
</script>
